package fr.appliClient.server.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import fr.appliClient.server.dao.VehicleDao;
import fr.appliClient.server.entity.Vehicle;

@Service
public class VehicleService {

	@Autowired
	private VehicleDao dao;

	/**
	 * Persist a Vehicle in data base
	 * 
	 * @param vehicleToPersist
	 * @throws Exception
	 */
	public Vehicle createVehicle(Vehicle vehicleToPersist) throws Exception {

		// Look if the vehicle already exist.
		if (this.dao.findByRegistration(vehicleToPersist.getRegistration()) != null) {
			throw new Exception("Vehicle already exist");
		}

		Vehicle savedEntity = this.dao.save(vehicleToPersist);
		if (savedEntity == null) {
			throw new Exception("fuck");
		}
		return savedEntity;
	}

	/**
	 * Get all Vehicles
	 * 
	 * @return
	 */
	public List<Vehicle> getVehicles() {

		return this.dao.readAll();
	}

	/**
	 * Find a vehicle.
	 * 
	 * @param vehicleId
	 * @return
	 */
	public Vehicle findById(Long vehicleId) {

		return this.dao.readByPrimaryKey(vehicleId);
	}

	/**
	 * Find a vehicle.
	 * 
	 * @param vehicleId
	 * @return
	 */
	public Vehicle findByRegistration(String registration) {

		return this.dao.findByRegistration(registration);
	}

}
